﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.ObjectRepository.CallbackManager;
using OverStore.Runtime.OverStoreContext;

namespace OverStore.Configuration.ObjectRepository.CallbackManager
{
    /// <summary>
    /// Composes few configurations for <see cref="ICallbackManager{TInstance}"/> components.
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    public class CompositeCallbackManagerConfiguration<TInstance> : IComponentConfiguration<ICallbackManager<TInstance>>
        where TInstance : class
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeCallbackManagerConfiguration&lt;TInstance&gt;"/> class.
        /// </summary>
        public CompositeCallbackManagerConfiguration()
        {
            Callbacks = new List<IComponentConfiguration<ICallbackManager<TInstance>>>();
        }

        /// <summary>
        /// Gets the callbacks.
        /// </summary>
        public List<IComponentConfiguration<ICallbackManager<TInstance>>> Callbacks { get; set; }

        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        public ICallbackManager<TInstance> CreateComponent(IOverStoreContext context)
        {
            return new CompositeCallbackManager<TInstance>(
                (Callbacks ?? Enumerable.Empty<IComponentConfiguration<ICallbackManager<TInstance>>>())
                        .Select(c => c.CreateComponent(context))
                        .ToList());
        }
    }
}
